package com.xpsoft.oa.workflow.event;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.api.listener.EventListener;
import org.jbpm.api.listener.EventListenerExecution;
import org.jbpm.api.model.OpenProcessInstance;

import com.xpsoft.core.util.AppUtil;
import com.xpsoft.oa.model.customer.SuppliersAssess;
import com.xpsoft.oa.model.flow.ProcessRun;
import com.xpsoft.oa.model.system.AppUser;
import com.xpsoft.oa.service.customer.SuppliersAssessService;
import com.xpsoft.oa.service.flow.ProcessRunService;

public class SuppliersAssessFinishListener implements EventListener{
	private Log logger=LogFactory.getLog(SuppliersAssessFinishListener.class);
	@Override
	public void notify(EventListenerExecution execution) throws Exception {
		//获取表单参数
		OpenProcessInstance pi=execution.getProcessInstance();
		String suppliersId = (String)pi.getVariable("suppliersAssess.suppliersId");
		String suppliersName = (String)pi.getVariable("suppliersAssess.suppliersName");
		String suppliersType = (String)pi.getVariable("suppliersAssess.suppliersType");
		String supplierAddress = (String)pi.getVariable("suppliersAssess.supplierAddress");
		String suppliersbank = (String)pi.getVariable("suppliersAssess.suppliersbank");
		String bankAccount = (String)pi.getVariable("suppliersAssess.bankAccount");
		String supplierContacter = (String)pi.getVariable("suppliersAssess.supplierContacter");
		String supplierPhone = (String)pi.getVariable("suppliersAssess.supplierPhone");
		String supplierLevel = (String)pi.getVariable("suppliersAssess.supplierLevel");
		String remark = (String)pi.getVariable("suppliersAssess.remark");
		
		SuppliersAssessService suppliersAssessService = (SuppliersAssessService)AppUtil.getBean("suppliersAssessService");
		SuppliersAssess suppliersAssess = null;
		if(StringUtils.isNotBlank(suppliersId)){
			suppliersAssess = suppliersAssessService.get(Long.valueOf(suppliersId));
		}else{
			suppliersAssess = new SuppliersAssess();
		}
		suppliersAssess.setSuppliersName(suppliersName);
		suppliersAssess.setSuppliersType(suppliersType);
		suppliersAssess.setSupplierAddress(supplierAddress);
		suppliersAssess.setSuppliersbank(suppliersbank);
		suppliersAssess.setBankAccount(bankAccount);
		suppliersAssess.setSupplierContacter(supplierContacter);
		suppliersAssess.setSupplierPhone(supplierPhone);
		suppliersAssess.setSupplierLevel(supplierLevel);
		suppliersAssess.setRemark(remark);
		suppliersAssess.setDelFlag("0");
		
		ProcessRunService processRunService=(ProcessRunService)AppUtil.getBean("processRunService");
		ProcessRun processRun=processRunService.getByExeId(pi.getId());
		suppliersAssess.setProcessRunId(processRun.getRunId());
		
		suppliersAssessService.save(suppliersAssess);
		
		
		//获取流程发起者对象
		AppUser flowStartUser=(AppUser)execution.getProcessInstance().getVariable("flowStartUser");
	
		//所有参与流程的都发消息
		AppUtil.sendSysShortMsg(execution.getProcessInstance().getId(), flowStartUser);
	}

}
